package com.apanal.qlife.advice.service.impl;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.apanal.qlife.advice.dao.AdviceMapper;
import com.apanal.qlife.advice.dao.AdviceOrgAreaRelMapper;
import com.apanal.qlife.advice.model.Advice;
import com.apanal.qlife.advice.model.AdviceOrgAreaRel;
import com.apanal.qlife.advice.service.SysInformService;
import com.apanal.qlife.common.constants.AdviceConstants;
import com.apanal.qlife.common.constants.Constants;
import com.apanal.qlife.common.page.Page;
import com.apanal.qlife.common.page.PageReturn;
import com.apanal.qlife.common.util.IdWorkerFactory;
import com.apanal.qlife.org.model.Organization;
import com.apanal.qlife.sys.dao.RecordsMapper;
import com.apanal.qlife.sys.model.Records;
import com.apanal.qlife.sys.model.User;

@Service
public class SysInformServiceImpl implements SysInformService {

	@Autowired
	AdviceMapper mapper;

	@Autowired
	RecordsMapper  redcordsMapper;
	
	@Autowired
	AdviceOrgAreaRelMapper adrmapper;
	@Override
	public PageReturn<Advice> findInformBycond(Advice advice, Page page) {
		   int count = mapper.selectRelationInformForPageCount(advice, page);
		   List<Advice> list = mapper.selectRelationInfromForPage(advice, page);
		   return new PageReturn<Advice>(list, count, page, advice);
	}

	@Override
	public Advice findInformDetailById(Long advId) {
		
		return mapper.selectByPrimaryKey(advId);
	}

	@Override
	public int findReadLog(Long useId, Long advId) {
		
		return redcordsMapper.findReadLog(useId,advId);
	}

	@Override
	public String insertRecords(Records rc) {
		redcordsMapper.insert(rc);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public Advice findAnnounceById(Long advId) {
		
		return mapper.selectByPrimaryKey(advId);
	}

	@Override
	public String updateTotalCount(Advice enty) {
		mapper.updateByPrimaryKey(enty);
		
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public String insertAdvice(Advice advice,User user) throws ParseException {
		advice.setAdvType(AdviceConstants.SYS_INFORM);
		advice.setInsertBy(user.getUseId());
		advice.setAdvId(IdWorkerFactory.nextId());
		String startNewDate = advice.getStartDateNew();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date = format.parse(startNewDate);
		advice.setStartDate(date);
		advice.setLastUpdateBy(user.getUseId());
		advice.setNormal(Constants.NORMAL_TRUE);
		advice.setStatus(Constants.IS_VALID_TRUE);
		advice.setSetType(AdviceConstants.SET_TYPE_ORG);
		mapper.insert(advice);
		int setTypeDetalis = advice.getSetTypeDetail();
		// 新增中间表
		String orgIds = advice.getOrgIds();
		if (setTypeDetalis != 11) { // 推送方式非全部机构
			if (orgIds != null && !orgIds.equals("")) {
				String[] ary = orgIds.split(",");
				for (int i = 0; i < ary.length; i++) {
					Long orgid = Long.parseLong(ary[i]);
					AdviceOrgAreaRel arog = new AdviceOrgAreaRel();
					arog.setRelId(IdWorkerFactory.nextId());
					arog.setOrgId(orgid);
					arog.setAdvId(advice.getAdvId());
					arog.setInsertBy(user.getUseId());
					arog.setSetTypeDetail(advice.getSetTypeDetail());
					adrmapper.insert(arog);
				}
			}
		}
		return  Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public String updateInform(Advice ac,User user,Advice advice) throws ParseException {
		int oldSetTypeDetail = ac.getSetTypeDetail();
		ac.setLastUpdateBy(user.getUseId());
		ac.setAdvDetails(advice.getAdvDetails());
		ac.setAdvTitle(advice.getAdvTitle());
		ac.setSetTypeDetail(advice.getSetTypeDetail());
		String startNewDate = advice.getStartDateNew();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date = format.parse(startNewDate);
		ac.setStartDate(date);
		mapper.updateByPrimaryKey(ac);
		int setTypeDetalis = advice.getSetTypeDetail();
		// 新增中间表
    	// 删除之间投放数据
		Advice a = new Advice();
		a.setAdvId(ac.getAdvId());
		a.setSetTypeDetail(oldSetTypeDetail);
		adrmapper.deletCurrentOrel(a);
		String orgIds = advice.getOrgIds();
		if (setTypeDetalis != 11) { // 推送方式非全部机构
			if (orgIds != null && !orgIds.equals("")) {
				String[] ary = orgIds.split(",");
				for (int i = 0; i < ary.length; i++) {
					Long orgid = Long.parseLong(ary[i]);
					AdviceOrgAreaRel arog = new AdviceOrgAreaRel();
					arog.setRelId(IdWorkerFactory.nextId());
					arog.setOrgId(orgid);
					arog.setAdvId(advice.getAdvId());
					arog.setInsertBy(user.getUseId());
					arog.setSetTypeDetail(advice.getSetTypeDetail());
					adrmapper.insert(arog);
				}
			}
	}
		
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public PageReturn<Advice> findInformBycondNo(Advice advice, Page page) {
		int count = mapper.selectRelationInformForPageCountNo(advice, page);
		List<Advice> list = mapper.selectRelationInfromForPageNo(advice, page);
		return new PageReturn<Advice>(list, count, page, advice);
	}

	@Override
	public String modifyInform(Advice ac) {
		mapper.updateByPrimaryKey(ac);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public String deleteInform(Advice ac) {
		//mapper.updateByPrimaryKey(ac);
		mapper.deleteByPrimaryKey(ac.getAdvId());
		adrmapper.deleteByPrimaryKey(ac.getAdvId());
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public String insertAppAdvice(Advice advice,User user) throws ParseException {
		// 新增公告表
				advice.setAdvType(AdviceConstants.APP_INFORM);
				advice.setInsertBy(user.getUseId());
				String startNewDate = advice.getStartDateNew();
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
				Date date = format.parse(startNewDate);
				advice.setStartDate(date);
				advice.setLastUpdateBy(user.getUseId());
				advice.setNormal(Constants.NORMAL_TRUE);
				advice.setStatus(Constants.IS_VALID_TRUE);
				advice.setAdvId(IdWorkerFactory.nextId());
				mapper.insert(advice);
				int setTypeDetalis = advice.getSetTypeDetail();
				// 新增中间表
				int setType = advice.getSetType();
				if (setTypeDetalis != 11 && setType == 1) { // 推送方式非全部机构
					String orgIds = advice.getOrgIds();
					if (orgIds != null && !orgIds.equals("")) {
						String[] ary = orgIds.split(",");
						for (int i = 0; i < ary.length; i++) {
							Long orgid = Long.parseLong(ary[i]);
							AdviceOrgAreaRel arog = new AdviceOrgAreaRel();
							if (setTypeDetalis == 15) {
								arog.setComId(orgid);
							} else {
								arog.setOrgId(orgid);
							}
							arog.setAdvId(advice.getAdvId());
							arog.setInsertBy(user.getUseId());
							arog.setSetTypeDetail(advice.getSetTypeDetail());
							arog.setRelId(IdWorkerFactory.nextId());
							adrmapper.insert(arog);
						}
					}
				} else if (setTypeDetalis != 21 && setType == 2) {
					String areids = advice.getOrgIds();
					if (areids != null && !areids.equals("")) {
						String[] ary = areids.split(",");
						for (int i = 0; i < ary.length; i++) {
							Long areid = Long.parseLong(ary[i]);
							AdviceOrgAreaRel arog = new AdviceOrgAreaRel();
							if (setTypeDetalis == 22) {
								arog.setProvinceId(areid);

							} else if (setTypeDetalis == 23) {
								arog.setCityId(areid);
							} else if (setTypeDetalis == 24) {
								arog.setAreaId(areid);
							}
							arog.setAdvId(advice.getAdvId());
							arog.setInsertBy(user.getUseId());
							arog.setSetTypeDetail(advice.getSetTypeDetail());
							arog.setRelId(IdWorkerFactory.nextId());
							adrmapper.insert(arog);
						}
					}

				}
		
				
		return  Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public String updateAppAonnce(Advice ac,User user,Advice advice) throws ParseException {
		
		// 更新公告表
				int oldSetTypeDetail = ac.getSetTypeDetail();
				ac.setLastUpdateBy(user.getUseId());
				ac.setAdvDetails(advice.getAdvDetails());
				ac.setAdvTitle(advice.getAdvTitle());
				ac.setSetType(advice.getSetType());
				ac.setPutInVersion(advice.getPutInVersion());
				ac.setSetTypeDetail(advice.getSetTypeDetail());
				String startNewDate = advice.getStartDateNew();
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
				Date date = format.parse(startNewDate);
				ac.setStartDate(date);
				mapper.updateByPrimaryKey(ac);
				int setTypeDetalis = advice.getSetTypeDetail();
				// 新增中间表
				// 删除之间投放数据
				Advice a = new Advice();
				a.setAdvId(ac.getAdvId());
				a.setSetTypeDetail(oldSetTypeDetail);
				adrmapper.deletCurrentOrel(a);
				int setType = advice.getSetType();
				if (setTypeDetalis != 11 && setType == 1) { // 推送方式非全部机构
					String orgIds = advice.getOrgIds();
					if (orgIds != null && !orgIds.equals("")) {
						String[] ary = orgIds.split(",");
						for (int i = 0; i < ary.length; i++) {
							Long orgid = Long.parseLong(ary[i]);
							AdviceOrgAreaRel arog = new AdviceOrgAreaRel();
							if (setTypeDetalis == 15) {
								arog.setComId(orgid);
							} else {
								arog.setOrgId(orgid);
							}
							arog.setAdvId(advice.getAdvId());
							arog.setInsertBy(user.getUseId());
							arog.setSetTypeDetail(advice.getSetTypeDetail());
							arog.setRelId(IdWorkerFactory.nextId());
							adrmapper.insert(arog);
						}
					}
				} else if (setTypeDetalis != 21 && setType == 2) {
					String areids = advice.getOrgIds();
					if (areids != null && !areids.equals("")) {
						String[] ary = areids.split(",");
						for (int i = 0; i < ary.length; i++) {
							Long areid = Long.parseLong(ary[i]);
							AdviceOrgAreaRel arog = new AdviceOrgAreaRel();
							if (setTypeDetalis == 22) {
								arog.setProvinceId(areid);

							} else if (setTypeDetalis == 23) {
								arog.setCityId(areid);
							} else if (setTypeDetalis == 24) {
								arog.setAreaId(areid);
							}
							arog.setAdvId(advice.getAdvId());
							arog.setInsertBy(user.getUseId());
							arog.setSetTypeDetail(advice.getSetTypeDetail());
							arog.setRelId(IdWorkerFactory.nextId());
							adrmapper.insert(arog);
						}
					}

				}
				
		return  Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public String modifyAppAonnce(Advice ac) {
		mapper.updateByPrimaryKey(ac);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public String deleteAppAonnce(Advice ac) {
		//mapper.updateByPrimaryKey(ac);
		mapper.deleteByPrimaryKey(ac.getAdvId());
		adrmapper.deleteByadvId(ac.getAdvId());
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public PageReturn<Advice> findInform(Advice advice, Page page,
			Organization org) {
	   int count = mapper.selectMessInformPageCount(advice, page,org);
	   List<Advice> list = mapper.selectMessInformPage(advice, page,org);
	   return new PageReturn<Advice>(list, count, page, advice);
	}



	

}